package com.up.vms.infrastructure.job;

import com.alibaba.fastjson.JSONObject;
import com.up.vms.infrastructure.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * Created by wangshd on 2018/12/5.
 */
@Slf4j
public class HttpClientUtil {

    public static ResponseEntity<String> post(String url, JobParameter obj, String type) throws Exception{
        try {
            if (CommonUtils.isNull(type)) {
                log.error("type不能为空");
                throw new Exception("type不能为空");
            }
            if (type.equals("json")) {
                return postJson(url, obj);
            } else if (type.equals("form")) {
                return postForm(url, obj);
            } else {
                log.error("不支持的mediaType类型：" + type);
                throw new Exception("不支持的mediaType类型：" + type);
            }
        }catch(Exception e){
            throw e;
        }
    }

    private static ResponseEntity<String> postJson(String url, JobParameter obj) throws Exception{
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        String jsonStr = JSONObject.toJSONString(obj);
        HttpEntity<String> JsonEntity = new HttpEntity<String>(jsonStr, headers);

        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, JsonEntity, String.class);

        return responseEntity;
    }

    private static ResponseEntity<String> postForm(String url, JobParameter obj)throws Exception{
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<String, Object>();
        map.add("jobGroup",String.valueOf(obj.getJobGroup()));
        map.add("jobCron",obj.getJobCron());
        map.add("jobDesc",obj.getJobDesc());
        map.add("executorHandler",obj.getExecutorHandler());
        map.add("executorRouteStrategy",obj.getExecutorRouteStrategy());
        map.add("executorBlockStrategy",obj.getExecutorBlockStrategy());
        map.add("executorFailStrategy",obj.getExecutorFailStrategy());
        map.add("author",obj.getAuthor());
        map.add("glueType",obj.getGlueType());
        map.add("executorParam",obj.getExecutorParam());


        //MediaType type = MediaType.parseMediaType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        //   String jsonStr = JSONObject.toJSONString(obj);

        HttpEntity<Map> formEntity = new HttpEntity<Map>(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, formEntity, String.class);

        return responseEntity;
    }
}
